package com.wx.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.wx.annotation.FitnessAnnotation;
import com.wx.constant.JedisConstant;
import com.wx.constant.MessageConstant;
import com.wx.entity.PageResult;
import com.wx.entity.QueryPageBean;
import com.wx.entity.Result;
import com.wx.pojo.Setmeal;
import com.wx.service.SetmealService;
import com.wx.utils.QiNiuFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;

@RestController
@RequestMapping("/pages/setmeal")
public class SetmealController {
    @Autowired
    @Qualifier("jedisPool")
    private JedisPool jedisPool;
    @Reference
    private SetmealService service;

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findll")
    public Result findAllSetmel(@RequestBody QueryPageBean queryPageBean){
        System.out.println(new Date());
        if(queryPageBean.getQueryString()==null){
            queryPageBean.setQueryString("");
        }
        try {
            PageResult allSetmeal = service.findAllSetmeal(queryPageBean);
            return new Result(true, MessageConstant.QUERY_SETMEALLIST_SUCCESS,allSetmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.QUERY_SETMEALLIST_FAIL);
        }

    }


    @RequestMapping("/findallcheckgroup")
    public Result findallcheckgroup(){
        try {
            PageResult pageResult = service.findAllCheckgroup();
            return  new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }
    /**
     * 文件上传
     */
    @RequestMapping("fileUpload")
    public Result fileUpload(@RequestParam("imgFile") MultipartFile imgFile){

        try {
            //得到文件的扩展名
            String originalFilename = imgFile.getOriginalFilename();
            int lastIndexOf = originalFilename.lastIndexOf(".");
            String substring = originalFilename.substring(lastIndexOf - 1);
            // 随机生成文件名
            String fileName = UUID.randomUUID().toString() + substring;
            // 得到上传文件的二进制数组
            byte[] bytes = imgFile.getBytes();
            // 文件上传
            QiNiuFileUpload.fileupload(bytes,fileName);
            // 文件上传成功把文件名存储到redis
            jedisPool.getResource().sadd(JedisConstant.JEDIS_SERVIC,fileName);
            // 把文件名返回前端
            return new Result(true,MessageConstant.PIC_UPLOAD_SUCCESS,fileName);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }

    }

    /**
     * 新增套餐
     */
    @RequestMapping("/add")
    @FitnessAnnotation
    public Result add(@RequestBody Setmeal setmeal,Integer[] ids){
        //获得添加的主键
        try {
            service.addSetmeal(setmeal,ids);
            System.out.println("模板测试");
         return    new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return    new Result(true,MessageConstant.ADD_SETMEAL_FAIL);
        }
    }
    /**
     * 删除套餐
     */
    @RequestMapping("/delete")
    @FitnessAnnotation
    public Result delete(Integer id){
        try {
            Result result = service.deleteSetemeal(id);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除失败");
        }

    }

    @RequestMapping("/getSetmealById")
    public Result getSetmealById(Integer id){
        try {
            Setmeal setmealById = service.getSetmealById(id);
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmealById);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    @RequestMapping("/edis")
    @FitnessAnnotation
    public Result edit(@RequestBody Setmeal setmeal,Integer[] id){
        System.out.println("aop模板测试");
        try {
            service.updateSetmeal(setmeal,id);
            return new Result(true,"编辑套餐成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"编辑套餐失败");
        }
    }
}
